package net.exoweb.animate.graphics
{
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;

    import net.exoweb.animate.common.Maths;
    import net.exoweb.animate.graphics.GShape;

    public class GButton extends GShape
    {
        protected var _text:TextField;
        protected var _border:Number;
        protected var _borderColor:uint;
        protected var _borderAlpha:Number;
        protected var _textColor:uint;

        public function GButton(text:String="", textColor:uint=0xFFFFFF, color:uint=0, border:Number=NaN, borderColor:uint=0XFFFFFF, borderAlpha:Number=1, bm:String="normal")
        {
            super(color, bm);
            _text = new TextField();
            _text.x = 20;
            _text.y = 5;
            _text.htmlText = "<b><font size='12'>" + text + "</font></b>";
            _text.textColor = textColor;
            _text.useRichTextClipboard = false;
            _border = border;
            _borderColor = borderColor;
            _borderAlpha = borderAlpha;
            alpha = 0.5;
            buttonMode = true;
            addChild(_text);

            addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
            addEventListener(MouseEvent.MOUSE_OUT, mouseOut);
        }

        public function set borderColor(v:uint):void
        {
            _borderColor = v;
            display();
        }

        public function get borderColor():uint
        {
            return _borderColor;
        }

        public function set textColor(v:uint):void
        {
            _textColor = v;
            display();
        }

        public function get textColor():uint
        {
            return _textColor;
        }

        public function fade(e:Event):void
        {
            if(alpha - 0.05 < 0.5)
            {
                alpha = 0.5;
                removeEventListener(Event.ENTER_FRAME, fade);
            }
            else
            {
                alpha -= 0.05;
            }
        }

        public function mouseOver(e:Event):void
        {
            alpha = 1;
            removeEventListener(Event.ENTER_FRAME, fade);
        }

        public function mouseOut(e:Event):void
        {
            addEventListener(Event.ENTER_FRAME, fade);
        }

        override public function display():void
        {
            graphics.clear();
            graphics.lineStyle(_border, _borderColor, _borderAlpha);
            graphics.beginFill(color);
            graphics.moveTo(0, 0);
            graphics.lineTo(150, 0);
            graphics.lineTo(160, 10);
            graphics.lineTo(160, 30);
            graphics.lineTo(10, 30);
            graphics.lineTo(0, 20);
            graphics.endFill();
        }
    }
}
